package com.coldfusiondocs.controller
{
    import com.coldfusiondocs.delegate.ILinkDelegate;
    import com.coldfusiondocs.event.GetLinkEvent;
    import com.coldfusiondocs.factories.LinkFactory;
    import com.coldfusiondocs.model.LinkModel;
    import com.coldfusiondocs.model.presentation.LinkPresentationModel;
    
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
    import mx.utils.ArrayUtil;
    
    import org.swizframework.controller.AbstractController;
    import org.swizframework.utils.chain.*;

    public class LinkController extends AbstractController
    {
        // PUBLIC PROPERTIES
        
        [Inject(source="linkDelegate")]
        public var delegate : ILinkDelegate;
        
        [Inject(source="linkModel")]
        public var linkModel : LinkModel;
        
 		[Inject(source="linkPresentationModel")]
		[Bindable]
		public var linkPresentationModel : LinkPresentationModel;       
        
        // CONSTRUCTOR
        
        public function LinkController()
        {
            super();
        }
        
        // PUBLIC METHODS
        [Mediate("GetLinkEvent.GET_LINK", properties="_item" )]
        public function getLinksByObject(item:String) : void
        {
        	this.linkModel.currentState = LinkModel.STATE_LOADING;
        	executeServiceCall( delegate.getLinksByObject( item ), getLinksHandler );
        } 
        
        [Mediate("AddLinkEvent.ADD_LINK", properties="link_data" )]
        public function addLink(item:Object) : void
        {
        	this.linkModel.currentState = LinkModel.STATE_LOADING;
        	executeServiceCall( delegate.addLink( item ), addLinksHandler );
        }   
        
        // EVENT HANDLERS
        private function addLinksHandler( event: ResultEvent ):void
        {
			var _str:String = event.result as String;

			// if successful response
			if (_str.toString() == 'Link Added!') {
				// link has been added, fire off a getLink event now to refresh
				dispatcher.dispatchEvent( new GetLinkEvent(GetLinkEvent.GET_LINK, linkPresentationModel.currentItem  ) );
			} else {
				// put the state back to how it was
				linkModel.currentState = LinkModel.STATE_READY;
			}        	
        	
        }              
         
        private function getLinksHandler( event : ResultEvent ):void
        {
			// get the data
			var tmpLinks:ArrayCollection = new ArrayCollection();
			if (event.result is ArrayCollection)
			{
				tmpLinks = ArrayCollection(event.result);
			} else {
				tmpLinks =  new ArrayCollection(mx.utils.ArrayUtil.toArray(event.result));
			}
			
			// parse the data
			var links:Array = LinkFactory.buildArrayofLinksFromResult(tmpLinks);
			
			// set the model
			this.linkModel.all_links = new ArrayCollection(links);
			
			// update state
			this.linkModel.currentState = LinkModel.STATE_READY;
        }
        
 
        
        
        

        
    }
}